package com.neu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.neu.App;
import com.neu.MainActivity;
import com.neu.R;
import com.neu.util.SpHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class WelcomeActivity extends AppCompatActivity  {

    private ViewPager viewPager;
    private TextView textView;
    private RadioGroup rdGroup;
    private int[] rbs = {R.id.rd1, R.id.rd2, R.id.rd3, R.id.rd4, R.id.rd5};
    private Button btnIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        String first=SpHelper.getString("first");
        if("first".equals(first)){
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }

        initView();
    }

    public void btnIndexClick(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        textView = (TextView) findViewById(R.id.textView);
        rdGroup = (RadioGroup) findViewById(R.id.rdGroup);
        viewPager.setAdapter(new PagerAdapter() {

            private int[] imgs = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5};

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                ImageView pic = new ImageView(getApplicationContext());
                pic.setScaleType(ImageView.ScaleType.CENTER_CROP);
                pic.setImageResource(imgs[position]);
                container.addView(pic);
                return pic;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }

            @Override
            public int getCount() {
                return imgs.length;
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return object == view;
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                rdGroup.check(rbs[position]);
                if (position + 1 == 5) {
                    textView.setVisibility(View.VISIBLE);
                    btnIndex.setVisibility(View.VISIBLE);
                }else{
                    textView.setVisibility(View.GONE);
                    btnIndex.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        btnIndex = (Button) findViewById(R.id.btnIndex);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setIp();
            }
        });

    }

    //设置IP
    private void setIp() {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("设置IP和端口号");
        View view=getLayoutInflater().inflate(R.layout.dialog_ip,null);
        final EditText txtIp=view.findViewById(R.id.txtIp);
        final EditText txtPort=view.findViewById(R.id.txtPort);
        builder.setView(view);
        builder.setPositiveButton("保存",null);
        final AlertDialog dialog=builder.create();
        dialog.show();

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!isboolIp(txtIp.getText().toString())){
                    Toast.makeText(App.context,"IP格式错误",Toast.LENGTH_LONG).show();
                    txtIp.setFocusable(true);
                }else{
                    if("".equals(txtPort.getText().toString())){
                        Toast.makeText(App.context,"端口不能为空",Toast.LENGTH_LONG).show();
                        txtIp.setFocusable(false);
                        txtPort.setFocusable(true);
                    }else{
                        //保存
                        String url="http://"+txtIp.getText().toString()+":"+txtPort.getText().toString();
                        SpHelper.setString("url",url);
                        Log.d("TAG", "onClick: "+SpHelper.getString("url"));

                        //设置第一此
                        SpHelper.setString("first","first");

                        //关闭dialog
                        dialog.dismiss();
                    }
                }
            }
        });
    }

    private boolean isboolIp(String ipAddress) {

        String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        Pattern pattern = Pattern.compile(ip);
        Matcher matcher = pattern.matcher(ipAddress);
        return matcher.matches();
    }



}